const express = require('express');
const app1 = express();
const bodyParser = require('body-parser')

app1.use(bodyParser.json());
//设置跨域访问
app1.all('*', function (req: any, res: any, next: any) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});





    const mysql = require("mysql");
    const db1 = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "",
        database: "www_website"
    });
    db1.connect((err: any) => {
        if (err) {
            console.error('MySql 连接失败 error connecting: ' + err.stack);
            return;
        }
        console.log('connected as id ' + db1.threadId);
    });

    app1.post('/imges', function (req, res) {
        let {wai,nei = 'li',elect = 'a',strurl,end = '.html',cont,resource = 1} = req.body.city;
        // console.log(req.body)
        // 'INSERT INTO `imagegrabpage`(`pageid`, `external`, `interior`, `details`, `starturl`, `endurl`, `pagecongt`, `access`, ` currentstate`) VALUES (0,"waibu","neir","shuju","kais","end","30","1","1")'
        // db1.query("INSERT INTO `imagegrabpage`(`pageid`, `external`, `interior`, `details`, `starturl`, `endurl`, `pagecongt`, `access`, ` currentstate`) VALUES (1,'"+wai+"','"+nei+",'"+elect+"','"+strurl+"','"+end+"','"+cont+"','"+resource+"','1')",(err)=>{
        //     db1.query( 'INSERT INTO `imagegrabpage`(`pageid`, `external`, `interior`, `details`, `starturl`, `endurl`, `pagecongt`, `access`, ` currentstate`) VALUES (0,"'+wai+'","'+nei+'","'+elect+'","'+strurl+'","'+end+'","'+cont+'","'+resource+'","1")',(err)=>{
        //     if(err){
        //         res.send(err);
        //     }else{
        //         res.send({ StatusCode: 200});
        //     }
        // })
        
        
    });



    // db1.end((err: any) => {
    //     if (err) {
    //         console.log('关闭数据库失败：', err)
    //     }
    //     console.log('The connection is terminated now  数据库 已关闭连接')
    // });

app1.listen(3000);
console.log('Listening on port 3000...');

// export { app, db }


